<template>
  <div class="login_container">
    <!--头像区域-->
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/timg.gif" alt="动画图片">
      </div>

      <!--登录表单区域-->
      <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <el-form-item class="login_btns">
          <el-button type="primary" @click="login('loginForm')">登录</el-button>
          <el-button @click="reset('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data () {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        rules: {
          username: [
            {
              required: true,
              message: '请输入登录名称',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 10,
              message: '长度在 3 到 10 个字符',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入登录密码',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 16,
              message: '长度在 6 到 16 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      login (loginForm) {
        var $this = this;
        this.$refs[loginForm].validate(async valid => {
          if (valid) {
            const {data:res} = await this.$http.post("login",this.loginForm);
            console.log(res.data)
            if(res.meta.status!=200){
              return $this.$message.error('用户名或者密码错误');
              //return "登录失败";
            }else{
              $this.$message({
                message: '恭喜您 '+`${$this.loginForm.username}`+'，登录成功',
                type: 'success'
              });
              sessionStorage.setItem("token",res.data.token);
              this.$router.push("/home");
              //return "登录成功";
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      reset(loginForm) {
        this.$refs[loginForm].resetFields();
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    height: 100%;
    background-color: #729bff;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #FFFFFF;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      margin: -65px auto;
      background-color: #ffffff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eeeeee;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0px 20px;
    box-sizing: border-box;
  }

  .login_btns {
    display: flex;
    justify-content: flex-end;
  }
</style>
